package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.alan.modules.bean.PageModel;
import net.alan.modules.bean.Result;
import net.alan.modules.bean.ResultUtil;
import net.alan.modules.bean.Setting;
import net.alan.modules.handle.BaseExecption;
import net.alan.modules.service.ISettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Api(tags = "参数设置管理")
@Controller
@RequestMapping(value = "setting")
public class SettingController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SettingController.class);
    @Autowired
    private ISettingService settingService;


    @ApiOperation(value = "修改", notes = "")
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public Result add(String name, String value) throws Exception {
        try {
            Setting setting = this.settingService.getById(Setting.class, name);
            setting.setValue(value);
            this.settingService.update(Setting.class,setting);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取", notes = "")
    @ApiImplicitParam(name = "name", value = "name", required = true, dataType = "String")
    @RequestMapping(value = "get/{name}", method = RequestMethod.GET)
    @ResponseBody
    public Setting get(@PathVariable("name") String name) throws Exception {
        return this.settingService.getById(Setting.class, name).copy();
    }

    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public PageModel list(PageModel pageModel) throws Exception {
        Page page = this.settingService.findAll(Setting.class,pageModel, Sort.Direction.DESC,"name");
        return pageModel.toPageModel(page);
    }
}
